package cn.cdtu.tibetplan.service.impl;

import java.util.List;

import cn.cdtu.tibetplan.mapper.RecruitmentMapper;
import cn.cdtu.tibetplan.pojo.PageResult;
import cn.cdtu.tibetplan.pojo.Recruitment;
import cn.cdtu.tibetplan.pojo.RecruitmentExample;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import cn.cdtu.tibetplan.pojo.RecruitmentExample.Criteria;
import cn.cdtu.tibetplan.service.RecruitmentService;

import org.springframework.stereotype.Service;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class RecruitmentServiceImpl implements RecruitmentService {

    @Autowired
    private RecruitmentMapper recruitmentMapper;

    /**
     * 查询全部
     */
    @Override
    public List<Recruitment> findAll() {
        return recruitmentMapper.selectByExample(null);
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Recruitment> page = (Page<Recruitment>) recruitmentMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(Recruitment recruitment) {
        recruitmentMapper.insert(recruitment);
    }

    /**
     * 修改
     */
    @Override
    public void update(Recruitment recruitment) {
        recruitmentMapper.updateByPrimaryKeySelective(recruitment);
    }

    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public Recruitment findOne(Long id) {
        return recruitmentMapper.selectByPrimaryKey(id.intValue());
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            recruitmentMapper.deleteByPrimaryKey(id.intValue());
        }
    }

    @Override
    public PageResult findPage(Recruitment recruitment, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        RecruitmentExample example = new RecruitmentExample();
        Criteria criteria = example.createCriteria();

        if (recruitment != null) {
            if (recruitment.getTitle() != null && recruitment.getTitle().length() > 0) {
                criteria.andTitleLike("%" + recruitment.getTitle() + "%");
            }
            if (recruitment.getInfo() != null && recruitment.getInfo().length() > 0) {
                criteria.andInfoLike("%" + recruitment.getInfo() + "%");
            }
            if (recruitment.getSchoolname() != null && recruitment.getSchoolname().length() > 0) {
                criteria.andSchoolnameLike("%" + recruitment.getSchoolname() + "%");
            }
            if (recruitment.getCreatetime() != null && recruitment.getCreatetime().length() > 0) {
                criteria.andCreatetimeLike("%" + recruitment.getCreatetime() + "%");
            }
            if (recruitment.getUpdatetime() != null && recruitment.getUpdatetime().length() > 0) {
                criteria.andUpdatetimeLike("%" + recruitment.getUpdatetime() + "%");
            }
            if (recruitment.getOperator() != null && recruitment.getOperator().length() > 0) {
                criteria.andOperatorLike("%" + recruitment.getOperator() + "%");
            }
            if (recruitment.getOther1() != null && recruitment.getOther1().length() > 0) {
                criteria.andOther1Like("%" + recruitment.getOther1() + "%");
            }
            if (recruitment.getOther2() != null && recruitment.getOther2().length() > 0) {
                criteria.andOther2Like("%" + recruitment.getOther2() + "%");
            }
            if (recruitment.getOther3() != null && recruitment.getOther3().length() > 0) {
                criteria.andOther3Like("%" + recruitment.getOther3() + "%");
            }

        }

        Page<Recruitment> page = (Page<Recruitment>) recruitmentMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

}
